package com.hnxxxy.jfmgr.service.Impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.jfmgr.mapper.JfBackstageMapper;
import com.hnxxxy.jfmgr.pojo.Jf_attachment;
import com.hnxxxy.jfmgr.pojo.Jf_conf;
import com.hnxxxy.jfmgr.pojo.Jf_detail;
import com.hnxxxy.jfmgr.service.JfBackstageService;
import com.hnxxxy.sysmgr.pojo.Staff;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @author：张浩东
 * @Phone：15096207164
 * @QQ：1367048164
 * @E-mail：1367048164@qq.com
 * @date:2020-05-19 16:33
 */

@Service
@Slf4j
public class JfBackstageServiceImpl implements JfBackstageService {

    @Autowired
    JfBackstageMapper jfBackstageMapper;

    /**
     * 查询父类加分信息
     * @return
     */
    @Override
    public List<Jf_conf> findAllJfConfFatherInfo() { return jfBackstageMapper.findAllJfConfFatherInfo(); }

    /**
     * 根据父id查询子类加分信息
     * @param id 父id
     * @return 子类加分信息
     */
    @Override
    public List<Jf_conf> queryJfConfSonInfo(int id) {
        return jfBackstageMapper.queryJfConfSonInfo(id);
    }

    /**
     * 根据父id查询子类加分信息
     * @param id 父id
     * @return 子类加分信息
     */
    @Override
    public Jf_conf queryJfConfPresentInfo(int id) {
        return jfBackstageMapper.queryJfConfPresentInfo(id);
    }

    /**
     * 证书信息录入
     * @param jf_detail  证书信息
     * @return 操作成功或失败的JSON
     */
    @Override
    @Transactional
    public int insertJfDetailCertificateInfo(Jf_detail jf_detail,String image) {
        AjaxRtnEntity ajaxRtnEntity = new AjaxRtnEntity();
        Jf_attachment jf_attachment;
        Subject subject = SecurityUtils.getSubject();
        Staff staff = (Staff) subject.getPrincipal();

        try{
            jf_detail.setUserId(staff.getUserid());
            jf_detail.setStatus(1);
            jfBackstageMapper.insertJfDetailCertificateInfo(jf_detail);

            jf_attachment = new Jf_attachment(staff.getUserid(),image);
            jf_attachment.setJfDetailId(jf_detail.getId());
            jfBackstageMapper.insertJfAttachmentInfo(jf_attachment);

            return ajaxRtnEntity.success;
        }catch (Exception e){
            log.info("-->证书信息录入异常：" + e.toString());
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ajaxRtnEntity.error;
        }
    }

}
